import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:kuzlak_ui/model/kelement.dart';

class BuildView {
 static Widget preview(KElement element) {
    return buildWidget(element);
  }
 static Widget buildWidget(KElement element) {
   switch (element.tag) {
      case 'scaffold':
        return KScaffold(element);
        break;
      default:
        return const Text('Not Found');
   }
  }

  static Widget KScaffold(KElement element) {
  KElement? appBar = element.appBar ;
   Map<String, dynamic>? attributes=appBar?.attributes;
   bool? showAppBar =appBar?.isShow;
  // 定义局部变量，明确逻辑分支
  final Color appBarBackgroundColor = appBar != null
      ? (appBar.color != null
      ? hexToColor(appBar.color!)
      : Colors.transparent)
      : Colors.transparent;
    return Scaffold(
      appBar:showAppBar==true? AppBar(
        title: Text(attributes!['title']),
       backgroundColor:appBarBackgroundColor,
        actions: [
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {},
          ),
          IconButton(
            icon: Icon(Icons.more_vert),
            onPressed: () {},
          ),
        ],
        leading: IconButton(
          icon: Icon(Icons.menu),
          onPressed: () {},
        ),
        
      ):null,
      backgroundColor: element.color!=null?hexToColor(element.color!) : Colors.transparent,
    );
  }

 static Color hexToColor(String hex) {
   // 移除可能存在的前缀 `#`
   hex = hex.replaceFirst('#', '');
   // 解析十六进制字符串为整数，并添加默认的 alpha 通道值 `FF`（不透明）
   return Color(int.parse('FF$hex', radix: 16));
 }
}
